import {getGoodsInfoXCX,miaoshaPay,getAddressList} from '../../../Tools/HTTPOpertion.js';
var _lastTime = null
function throttle(fn, gapTime) {
  if (gapTime == null || gapTime == undefined) {
    gapTime = 1000
  }

  // let _lastTime = null

  // 返回新的函数
  return function () {
    let _nowTime = + new Date()
    console.log(_nowTime, _lastTime, gapTime);
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      fn.apply(this, arguments)   //将this和参数传给原函数
      _lastTime = _nowTime
    }
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodInfo: {},
    cnd: '',
    addressInfo: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const orderId = options.orderId
    // const orderId = '956'
    this.getAddress();
    this.getGoodInfo(orderId);
  },
  onShow: function () {
    this.getAddress();
  },
  getGoodInfo: function (orderId) {
    // 获取支付详情
    const token = wx.getStorageSync('token');
    const obj = {
      cnd: orderId // 订单ID
    }
    if (token) {
      obj.token = token;
    }

    const that = this;
    getGoodsInfoXCX(obj).then(function (res) {
      if (res.code === 0) {
        that.setData({
          goodInfo: res.data,
          'goodInfo.extra': res.data.currency / 100
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    }).catch(function (err) {
    });
  },
  //编辑地址
  bjAddress: function () {
    var obj = this.data.addressInfo;
    if (obj.id) {
      wx.navigateTo({
        url: '/pages/others/WriteAddress/WriteAddress?address_id=' + obj.id + '&create_person_id=' + obj.person_id + '&group_buy_id=' + 0 + '&a1=' + obj.name + '&a2=' + obj.phone + '&a3=' + obj.address
      })
    } else {
      wx.navigateTo({
        url: '/pages/others/WriteAddress/WriteAddress?address_id=' + '' + '&create_person_id=' + '' + '&group_buy_id=' + '' + '&a1=' + '' + '&a2=' + '' + '&a3=' + ''
      })
    }

  },
  toPay() {
    throttle(() => this.toPay1(), 1000)();
  },
  //立即支付
  toPay1: function () {
    var that = this;
    if (!this.data.addressInfo.id && this.data.goodInfo.delivery_method != 2) {
      wx.showToast({
        title: '请先添加地址',
        icon: 'none'
      });
      return
    };
    if (that.data.goodInfo.delivery_method==2){
      that.data.addressInfo.id=-1;
    }
    var obj = {
      cnd: that.data.goodInfo.id,
      address_id: that.data.addressInfo.id,
      token: wx.getStorageSync('token')
    };
    miaoshaPay(obj).then(function (res) {
      console.warn(obj)
      console.log(res)
      if (res.code === 0) {
        // 调起支付
        const data = res.data;
        wx.requestPayment({
          'appId': data.appId,
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': data.signType,
          'paySign': data.paySign,
          'success': function (res) {
            wx.redirectTo({
              url: `/pages/active/miaosha/miaosha?orderId=${that.data.goodInfo.orderId}`
            });
          },
          'fail': function (res) {
            console.log('res:', res)
            wx.showToast({
              title: "已取消支付",
              icon: 'none',
              duration: 1000
            })
          }
        })
      } else if (res.code === 2) {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        });
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/main/spellGroupHome/spellGroupHome'
          });
        }, 2000);
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    }).catch(function (err) {
    });
  },
  getAddress: function () {
    // 获取收获地址
    const that = this;
    getAddressList({}).then(function (res) {
      if (res.code === 0) {
        that.setData({
          addressInfo: res.data || {}
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }

    }).catch(function (err) {
    });
  }
})
